package org.ajc.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.ajc.org.dto.RoleDTO;
import org.ajc.base.annotation.AjcPermission;
import org.ajc.base.util.AjaxResult;
import org.ajc.base.util.PageInfo;
import org.ajc.org.domain.Employee;
import org.ajc.org.query.EmployeeQuery;
import org.ajc.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/employee")
@Api(value = "员工管理接口",description = "员工管理")
@AjcPermission(name = "员工管理",desc = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**新增或修改*/
    @PutMapping
    @ApiOperation("员工新增和修改接口")
    @AjcPermission(name = "员工新增或修改",desc = "员工新增和修改接口")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (Objects.isNull(employee.getId())){
                //id为空 新增
                employeeService.insert(employee);
            }else{
                //id不为空 修改
                employeeService.update(employee);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**查询全部*/
    @GetMapping
    @ApiOperation("员工查询全部接口")
    @AjcPermission(name = "员工查询全部",desc = "查询全部员工")
    public AjaxResult selectAll(){
        try {
            List<Employee> employee = employeeService.selectAll();
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**查询指定*/
    @GetMapping("/{id}")
    @ApiOperation("根据指定id查询指定员工")
    @AjcPermission(name = "查询指定员工",desc = "根据指定id查询指定员工")
    public AjaxResult selectById(@PathVariable("id")String id){
        try {
            Employee employee = employeeService.selectById(id);
            if (Objects.nonNull(employee)){
                return AjaxResult.success(employee);
            }else{
                return AjaxResult.query(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**删除指定*/
    @DeleteMapping("/{id}")
    @ApiOperation("根据指定id删除指定员工")
    @AjcPermission(name = "删除指定",desc = "根据指定id删除指定员工")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**删除多个*/
    @PatchMapping
    @ApiOperation("删除多个员工")
    @AjcPermission(name = "删除多个",desc = "删除多个员工")
    public AjaxResult batchDelete(@RequestBody List<Serializable> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**员工分页查询*/
    @PostMapping
    @ApiOperation("分页查询")
    @AjcPermission(name = "分页查询",desc = "分页查询员工")
    public AjaxResult queryPage(@RequestBody EmployeeQuery employeeQuery){
        try {
            PageInfo pageInfo = employeeService.queryPage(employeeQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**查询上级部门*/
    @GetMapping("/deptTree")
    @ApiOperation("查询员工下级部门")
    @AjcPermission(name = "查询员工下级部门",desc = "查询员工下级部门")
    public AjaxResult deptTree(){
        try {
            List<Employee> deptTree = employeeService.deptTree();
            if (Objects.nonNull(deptTree)){
                return AjaxResult.success(deptTree);
            }else{
                return AjaxResult.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "查询员工的角色")
    @PostMapping("/empRole/{id}")
    @AjcPermission(name = "查员工角色",desc = "根据id查员工所拥有的角色")
    public AjaxResult queryRoleByEmpId(@PathVariable("id") Long id){
        try {
            List<String> roleSns = employeeService.queryRoleByEmpId(id);
            System.out.println(roleSns);
            return AjaxResult.success(roleSns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询员工的角色失败"+e.getMessage());
        }
    }

    @ApiOperation(value = "授予员工角色")
    @PostMapping("/empSetRole")
    @AjcPermission(name = "授予员工角色",desc = "授予员工角色")
    public AjaxResult setRole(@RequestBody RoleDTO roleDTO){
        try {
            employeeService.setRole(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("授予员工角色失败"+e.getMessage());
        }
    }



    @PostMapping("/upload")
    @AjcPermission(name = "文件导入", desc = "通过文件导入用户信息")
    public AjaxResult upload(MultipartFile file) {
        try {
            Object upload = employeeService.upload(file);
            return AjaxResult.success(upload);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @PostMapping("/download")
    @AjcPermission(name = "导出数据", desc = "将用户信息列表导出为xlsx文件")
    public ResponseEntity<Resource> download(@RequestBody EmployeeQuery empQuery) {
        try {
            List<Employee> employees = employeeService.selectByKey(empQuery);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            employeeService.download(employees, outputStream);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=downEmp.xlsx")
                    .body(new ByteArrayResource(outputStream.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    @PostMapping("/download1")
    @AjcPermission(name = "导出数据", desc = "将用户错误信息列表导出为xlsx文件")
    public ResponseEntity<Resource> download(@RequestBody List<Employee> employees) {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            employeeService.download(employees, outputStream);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=downEmp.xlsx")
                    .body(new ByteArrayResource(outputStream.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

}
